home *** CD-ROM | disk | FTP | other *** search
/ Gigarom 4 / Mac Giga-ROM 4.0 - 1993.toast / FILES / DAS / A-C / Ascii / ASCII.C < prev    next >
Encoding:
C/C++ Source or Header  |  1985-11-02  |  14.0 KB  |  595 lines  |  [TEXT/????]

  1. #include <mac/quickdraw.h>
  2. #include <mac/toolintf.h>
  3. #include <mac/osintf.h>
  4. #include <mac/packintf.h>
  5. #include "ascii.h"
  6.  
  7. main () {
  8.     /* Standard initialisation */
  9.     struct QDVar QDVar;
  10.  
  11.     QD = &QDVar;
  12.     InitGraf (&thePort);
  13.     InitFonts ();
  14.     FlushEvents (everyEvent, 0);
  15.     InitWindows ();
  16.     InitMenus ();
  17.     TEInit ();
  18.     InitDialogs ( (ProcPtr) 0);
  19. /*  InitCursor ();                       Done in the LOOP  */
  20.  
  21.     /* Application dependent initialisation */
  22.     init_the_w ();
  23.     init_the_text ();
  24.     init_the_menu ();
  25.     init_the_scrap ();
  26.     init_the_cursor ();
  27.     file_num = NONE;
  28.  
  29. /* The Main Event LOOP */
  30.     while (1){
  31.     TEIdle (h_text);
  32.     SystemTask ();
  33.  
  34.     VOID GetNextEvent (everyEvent, &my_event);
  35.  
  36.     if (the_w == FrontWindow ()){
  37.         adjust_cursor ();
  38.         mouse_p.h = my_event.where.h;
  39.         mouse_p.v = my_event.where.v;
  40.         GlobalToLocal (&mouse_p);
  41.     }
  42.  
  43.     switch (my_event.what){
  44.         case mouseDown:
  45.         switch (FindWindow (&my_event.where, &which_w)){
  46.             case inMenuBar:
  47.             do_command (MenuSelect (&my_event.where));
  48.             break;
  49.             case inContent:
  50.             if (the_w == FrontWindow ())
  51.                 the_w_click ();
  52.             else
  53.                 SelectWindow (which_w);
  54.             break;
  55.             case inDrag:
  56.             break;    /* No drag done */
  57.             case inGoAway:
  58.             break;    /* Ne exit availible */
  59.             case inSysWindow:
  60.             SystemClick (&my_event, which_w);
  61.             break;
  62.         }
  63.         break;
  64.         case autoKey:
  65.         case keyDown:
  66.         if (the_w == FrontWindow ())
  67.             do_key ();
  68.         break;
  69.         case activateEvt:
  70.         if (the_w == (WindowPtr) my_event.message){
  71.             TEActivate (h_text);
  72.             DisableItem (my_menus[2], TEUNDO);
  73.             EnableItem (my_menus[3], 0);
  74.             EnableItem (my_menus[4], 0);
  75.         }
  76.         else{
  77.             TEDeactivate (h_text);
  78.             EnableItem (my_menus[2], TEUNDO);
  79.             DisableItem (my_menus[3], 0);
  80.             DisableItem (my_menus[4], 0);
  81.         }
  82.         DrawMenuBar ();
  83.         break;
  84.         case updateEvt:
  85.         if (the_w == (WindowPtr) my_event.message){
  86.             BeginUpdate (the_w);
  87.             SetCursor (*watch_c);
  88.             draw_the_w ();
  89.             TEUpdate (&text_r, h_text);
  90.             ShowCursor ();
  91.             EndUpdate (the_w);
  92.         }
  93.         break;
  94.      }
  95.     }
  96. }
  97.  
  98. init_the_w (){
  99.     int x, y;
  100.  
  101.     the_w = GetNewWindow (WINDOW, &w_record,  (WindowPtr) - 1);
  102.     SetRect (&ascii_r, 18, 18, 418, 320);
  103.     SetRect (&text_r, 425, 18, 500, 317);
  104.     SetRect (&view_r, 420, 0, 512, 322);
  105.     for (x = 0; x < 16; x++)
  106.     for (y = 0; y < 16; y++)
  107.         SetRect (&char_r[x][y], 18+25*x, 18+19*y, 43+25*x, 37+19*y);
  108.     SetPort (the_w);
  109. }
  110.  
  111. init_the_text (){
  112.     char temp_str[256];
  113.  
  114.     h_text = TENew (&text_r, &view_r);               /* view_r > text_r */
  115.     h_authortext = TENew (&text_r, &view_r);
  116.     sample_str = GetString(SAMPLESTR);
  117.     author_str = GetString(AUTHORSTR);
  118.     p2c_string(*sample_str, temp_str);
  119.     TESetText (temp_str, (int) (*sample_str)[0], h_text);
  120.     p2c_string(*author_str, temp_str);
  121.     TESetText (temp_str, (int) (*author_str)[0], h_authortext);
  122. }
  123.  
  124. change_font (size) int size;{   /* 0 means new font => chose smallest */
  125.     int font, i;
  126.     TEHandle TEnew_h;
  127.     Handle save_h;
  128.     short int save_length,save_start, save_end;
  129.  
  130.     for (i = 1; i <= num_of_sizes; i++)
  131.     CheckItem (my_menus[4], i, 0);
  132.     GetItem (my_menus[3], which_font, font_name);
  133.     GetFNum (font_name, &font);
  134.     if (! size){
  135.     for (i = 1; i <= num_of_sizes; i++)
  136.         if (RealFont (font, font_sizes[i])){
  137.         size = font_sizes[i];
  138.         break;
  139.         }
  140.     if (size > 18)
  141.         size = 12;
  142.     }
  143.     if (! size)
  144.     size = 12;
  145.  
  146.     TextFont(font);
  147.     TextSize(size);
  148.  
  149.     save_h = (*h_text)->hText;              /* Save handle to text */
  150.     save_length = (*h_text)->teLength;
  151.     save_start = (*h_text)->selStart;
  152.     save_end = (*h_text)->selEnd;
  153.  
  154.     TEnew_h = TENew(&text_r, &view_r);      /* Create new record */
  155.  
  156.     (*h_text)->hText = (*TEnew_h)->hText;   /* Change handle in old record */
  157.     TEDispose(h_text);                      /* Dispose of old record */
  158.  
  159.     h_text = TEnew_h;                       /* Rename new record */
  160.  
  161.     (*h_text)->hText = save_h;              /* Restore handle to text */
  162.     (*h_text)->teLength = save_length;
  163.     (*h_text)->selStart = save_start;
  164.     (*h_text)->selEnd = save_end;
  165.  
  166.     TEActivate(h_text);
  167.     TECalText (h_text);
  168.  
  169.     for (i = 1; i <= num_of_sizes; i++)
  170.     if (the_w->txSize == font_sizes[i])
  171.         CheckItem (my_menus[4], i, 1);
  172. }
  173.  
  174. init_the_menu (){
  175.     my_menus[0] = GetMenu(APPLE);
  176.     (**my_menus[0]).menuData.s[1] = '\024'; /* Silly thing this !! */
  177.     AddResMenu (my_menus[0], "DRVR");
  178.     InsertMenu (my_menus[0], 0);
  179.  
  180.     my_menus[1] = GetMenu (FILE);
  181.     InsertMenu (my_menus[1], 0);
  182.  
  183.     my_menus[2] = GetMenu (EDIT);
  184.     InsertMenu (my_menus[2], 0);
  185.  
  186.     add_font_menu ();
  187.  
  188.     DrawMenuBar ();
  189. }
  190.  
  191. add_font_menu (){
  192.     my_menus[3] = NewMenu (FONTS, "Font");
  193.     AddResMenu (my_menus[3], "FONT");
  194.     InsertMenu (my_menus[3], 0);
  195.     which_font = 1;
  196.     CheckItem (my_menus[3], which_font, 1);
  197.     change_font (12);
  198.     add_size_menu ();
  199.     change_font (0);
  200. }
  201.  
  202. add_size_menu (){
  203.     char name[64];
  204.     int i;
  205.  
  206.     my_menus[4] = NewMenu (FONTSIZE, font_name);
  207.  
  208.     num_of_sizes = 0;
  209.     for (i = 1; i < 128; i++)
  210.     if (RealFont (the_w->txFont, i)){
  211.         NumToString (i, name);
  212.         append_string ("<O", name);
  213.         AppendMenu (my_menus[4], name);
  214.         font_sizes[++num_of_sizes] = i;
  215.     }
  216.     else
  217.         if (i == 9 || i == 10 || i == 12 || i == 14 || i == 18){
  218.         NumToString (i, name);
  219.         AppendMenu (my_menus[4], name);
  220.         font_sizes[++num_of_sizes] = i;
  221.         }
  222.     InsertMenu (my_menus[4], 0);
  223.     for (i = 1; i <= num_of_sizes; i++)
  224.     if (the_w->txSize == font_sizes[i])
  225.         CheckItem (my_menus[4], i, 1);
  226. }
  227.  
  228. append_string (app, str) char *app, *str;{
  229.     int i, j;
  230.  
  231.     i = 0;
  232.     while (str[i++] != 0 && i < 60);
  233.     i--;
  234.  
  235.     j = 0;
  236.     while (app[j] != 0 && i + j < 60)
  237.     str[i++] = app[j++];
  238.     str[i] = 0;
  239. }
  240.  
  241. init_the_cursor (){
  242.     finger_c = GetCursor (FINGER); HNoPurge ((Handle) finger_c);
  243.     watch_c = GetCursor (WATCH); HNoPurge ((Handle) watch_c);
  244.     bar_c = GetCursor (BAR); HNoPurge ((Handle) bar_c);
  245. }
  246.  
  247. init_the_scrap (){
  248.     ScrapStuff *scrap_stuff;
  249.  
  250.     scrap_stuff = InfoScrap ();
  251.     scrap_count = ! ( scrap_stuff->scrapCount );
  252. /*  get_scrap ();                Dosn't work yet   */
  253. }
  254.  
  255. do_key (){
  256.     char the_char;
  257.  
  258.     the_char = my_event.message & 0xff;
  259.     if(my_event.modifiers & cmdKey)
  260.     do_command(MenuKey(the_char));
  261.     else{
  262.     InvertRect (&char_r[(the_char>>4) & 0xf][the_char & 0xf]);
  263.     TEKey (the_char, h_text);
  264.     InvertRect (&char_r[(the_char>>4) & 0xf][the_char & 0xf]);
  265.     }
  266. }
  267.  
  268. draw_the_w (){
  269.     int x, y, save_font, save_size;
  270.  
  271.     EraseRect (&the_w->portRect);
  272.     FrameRect (&ascii_r);
  273.  
  274.     for (x = 0; x < 16; x++){
  275.     save_font = the_w->txFont;
  276.     save_size = the_w->txSize;
  277.     the_w->txFont = systemFont;
  278.     the_w->txSize = 12;
  279.     MoveTo (5, char_r[0][x].top + 14);
  280.     if (x < 10)
  281.         DrawChar ( (char) ( x + '0') );
  282.     else
  283.         DrawChar ( (char) ( x + 'a' - 10) );
  284.     MoveTo (char_r[x][0].topLeft.h + 5, 10);
  285.     if (x < 10)
  286.         DrawChar ( (char) ( x + '0') );
  287.     else
  288.         DrawChar ( (char) ( x + 'a' - 10) );
  289.     the_w->txFont = save_font;
  290.     the_w->txSize = save_size;
  291.     for (y = 0; y < 16; y++){
  292.         MoveTo (char_r[x][y].topLeft.h + 5, char_r[x][y].top + 14);
  293.         DrawChar ( (char) ( x*16 + y ) );
  294.     }
  295.     }
  296. }
  297.  
  298. do_command (help) long help;{
  299.     char name[64];
  300.  
  301.     the_menu =  HiWord (help);
  302.     the_item =  LoWord (help);
  303.     switch (the_menu){
  304.     case APPLE:
  305.         if(the_item == ABOUT){
  306.         about();
  307.         break;
  308.         }
  309.         if(the_item == ABOUT + 1)
  310.         break;
  311.         GetItem (my_menus[0], the_item, name);
  312.         {
  313.         GrafPtr save_port;
  314.         char *da_name ();
  315.  
  316.         GetPort (&save_port);
  317.         if (GetNamedResource ("DRVR", name))/* Without leading zero */
  318.             VOID OpenDeskAcc (name);
  319.         else                                /* With leading zero    */
  320.             VOID OpenDeskAcc (da_name (name));
  321.         SetPort (save_port);
  322.         }
  323.         break;
  324.     case FILE:
  325.         switch (the_item){
  326.         case OPEN1:
  327.         case OPEN2:
  328.         case OPEN3:
  329.             open_file (the_item);
  330.             break;
  331.         case QUIT:
  332.             VOID LoadScrap ();
  333.             ExitToShell ();
  334.             break;
  335.         }
  336.         break;
  337.     case EDIT:
  338.         if (SystemEdit (the_item - 1))
  339.         break;
  340.         switch (the_item){
  341.         case TECUT:
  342.             TECut (h_text);
  343.             put_scrap ();
  344.             break;
  345.         case TECOPY:
  346.             TECopy (h_text);
  347.             put_scrap ();
  348.             break;
  349.         case TEPASTE:
  350.             get_scrap ();
  351.             TEPaste (h_text);
  352.             break;
  353.         case TECLEAR:
  354.             TEDelete (h_text);
  355.             break;
  356.         }
  357.         break;
  358.     case FONTS:
  359.         CheckItem (my_menus[3], which_font, 0);
  360.         which_font = the_item;
  361.         change_font (0);
  362.         CheckItem (my_menus[3], which_font, 1);
  363.  
  364.         num_of_sizes = 0;
  365.         DeleteMenu (FONTSIZE);
  366.         add_size_menu ();
  367.         DrawMenuBar ();
  368.  
  369.         InvalRect (&the_w->portRect);
  370.         break;
  371.     case FONTSIZE:
  372.         change_font (font_sizes[the_item]);
  373.         InvalRect (&the_w->portRect);
  374.     break;
  375.     }
  376.     HiliteMenu (0);
  377. }
  378.  
  379. adjust_cursor (){
  380.     if (PtInRect (&mouse_p, &ascii_r)){
  381.     SetCursor (*finger_c);
  382.     return;
  383.     }
  384.     if (PtInRect (&mouse_p, &text_r)){
  385.     SetCursor (*bar_c);
  386.     return;
  387.     }
  388.     InitCursor ();
  389. }
  390.  
  391. about(){
  392.     EraseRect (&text_r);
  393.     TEUpdate (&text_r, h_authortext);
  394.     while (! Button ());
  395.     while (Button ());
  396.     EraseRect (&text_r);
  397.     TEUpdate (&text_r, h_text);
  398.     FlushEvents (everyEvent, 0);
  399. }
  400.  
  401. the_w_click (){
  402.     int x, y;
  403.  
  404.     if (PtInRect (&mouse_p, &ascii_r)){
  405.     x =  (mouse_p.h - 18) / 25;
  406.     y =  (mouse_p.v - 18) / 19;
  407.     InvertRect (&char_r[x][y]);
  408.     TEKey ( (char) ( x*16 + y ) , h_text);
  409.     InvertRect (&char_r[x][y]);
  410.     }
  411.     if (PtInRect (&mouse_p, &text_r))
  412.     if(my_event.modifiers & shiftKey)
  413.         TEClick (&mouse_p, 1, h_text);
  414.     else
  415.         TEClick (&mouse_p, 0, h_text);
  416. }
  417.  
  418. put_scrap (){
  419.     ScrapStuff *scrap_stuff;
  420.  
  421.     VOID ZeroScrap ();
  422.     HLock (TEScrapHandle);
  423.     VOID PutScrap (GetHandleSize (TEScrapHandle), "TEXT", *TEScrapHandle);
  424.     HUnlock (TEScrapHandle);
  425.     scrap_stuff = InfoScrap ();
  426.     scrap_count = scrap_stuff->scrapCount;
  427. }
  428.  
  429. get_scrap (){
  430.     int off, len;
  431.     ScrapStuff *scrap_stuff;
  432.  
  433.     scrap_stuff = InfoScrap ();
  434.     if (scrap_count == scrap_stuff->scrapCount)
  435.     return;
  436.     scrap_count = scrap_stuff->scrapCount;
  437.     len = GetScrap (TEScrapHandle, "TEXT", &off);
  438.     if (len >= 0)
  439.         TEScrapLen = len;
  440.     /* Else, it was probably a PICT or other type of scrap;
  441.        in this case TEScrapHandle's contents has not been touched. */
  442. }
  443.  
  444. open_file (item) int item;{
  445.     Point top_left;
  446.     int file_filter1(), file_filter2();
  447.     OsType file_type;
  448.  
  449.     SetPt (&top_left, 50, 50);
  450.     switch(item){
  451.     case OPEN1:
  452.         SFGetFile (&top_left, "", file_filter1, -1,
  453.                (char *) 0, (ProcPtr) 0, &file_record);
  454.         break;
  455.     case OPEN2:
  456.         file_type.s[0] = 'F';
  457.         file_type.s[1] = 'F';
  458.         file_type.s[2] = 'I';
  459.         file_type.s[3] = 'L';
  460.         SFGetFile (&top_left, "", file_filter1, 1,
  461.                (char *) &file_type, (ProcPtr) 0, &file_record);
  462.         break;
  463.     case OPEN3:
  464.         SFGetFile (&top_left, "", file_filter2, -1,
  465.                (char *) 0, (ProcPtr) 0, &file_record);
  466.         break;
  467.     }
  468.     if (file_record.good){
  469.     if (file_num != NONE){
  470.         VOID SetVol ((char *) 0, volume_num);
  471.         CloseResFile (file_num);
  472.     }
  473.     volume_num = file_record.vRefNum;
  474.     VOID SetVol ((char *) 0, volume_num);
  475.     p2cstr ((char *) file_record.fName);
  476.     file_num = OpenResFile (file_record.fName);
  477.     DeleteMenu (FONTS);
  478.     DeleteMenu (FONTSIZE);
  479.     add_font_menu ();
  480.     DrawMenuBar ();
  481.     InvalRect (&the_w->portRect);
  482.     }
  483. }
  484.  
  485. /* This filter dosn't open System file, Ascii Application file and
  486.    files without resource fork. */
  487.  
  488.  
  489. file_filter1() {
  490.   struct pargs {
  491.     FileParam *paramBlock;
  492.   } pargs;
  493.   char *retval= (char *) getpargs(&pargs, sizeof pargs);
  494.   if (pargs.paramBlock->ioFlRLgLen == 0)
  495.     *retval= 1; /* TRUE => skip this file */
  496.   else{
  497.     char fdType[5], fdCreator[5];
  498.  
  499.     Os2c_string(pargs.paramBlock->ioFlFndrInfo.fdType, fdType);
  500.     Os2c_string(pargs.paramBlock->ioFlFndrInfo.fdCreator, fdCreator);
  501.     if(EqualString("ZSYS", fdType, 0, 0) &&
  502.        EqualString("MACS", fdCreator, 0, 0) ||
  503.        EqualString("APPL", fdType, 0, 0) &&
  504.        EqualString("AscI", fdCreator, 0, 0))
  505.       *retval= 1; /* TRUE => skip this file */
  506.     else
  507.       *retval= 0; /* FALSE => show this file*/
  508.   }
  509. }
  510.  
  511. /* This filter dosn't open System file, Ascii Application file,
  512.    files without resource fork and files whithout FONT resource. */
  513.  
  514.  
  515. file_filter2() {
  516.   struct pargs {
  517.     FileParam *paramBlock;
  518.   } pargs;
  519.   char *retval= (char *) getpargs(&pargs, sizeof pargs);
  520.   if (pargs.paramBlock->ioFlRLgLen == 0)
  521.     *retval= 1; /* TRUE => skip this file */
  522.   else{
  523.     char fdType[5], fdCreator[5];
  524.  
  525.     Os2c_string(pargs.paramBlock->ioFlFndrInfo.fdType, fdType);
  526.     Os2c_string(pargs.paramBlock->ioFlFndrInfo.fdCreator, fdCreator);
  527.     if(EqualString("ZSYS", fdType, 0, 0) &&
  528.        EqualString("MACS", fdCreator, 0, 0) ||
  529.        EqualString("APPL", fdType, 0, 0) &&
  530.        EqualString("AscI", fdCreator, 0, 0))
  531.       *retval= 1; /* TRUE => skip this file */
  532.     else{
  533.       int num_resources, f_num;
  534.       char f_name[256];
  535.  
  536.       VOID SetVol ((char *) 0, pargs.paramBlock->ioVRefNum);
  537.       num_resources = CountResources("FONT");
  538.       p2c_string((char *) pargs.paramBlock->ioNamePtr, f_name);
  539.       f_num = OpenResFile (f_name);
  540.       if(CountResources("FONT") > num_resources)
  541.     *retval= 0; /* FALSE => show this file*/
  542.       else
  543.     *retval= 1; /* TRUE => skip this file */
  544.       CloseResFile(f_num);
  545.     }
  546.   }
  547. }
  548.  
  549. /*
  550.  * Some desk accessories have weird names that begin with a leading zero.
  551.  * Since this fools the automatic C/Pascal string conversion stuff, we
  552.  * have this little gem;  given a C string, it returns a Pascal string
  553.  * with leading zero.
  554.  */
  555. char *da_name (s) char *s; {
  556.     static char ps[32];
  557.     int i = 1;
  558.     register char *cp,*dp;
  559.  
  560.     cp = s;
  561.     dp = &ps[2];
  562.     while ((*dp++ = *cp++))
  563.         i++;
  564.     ps[0] = i;
  565.     return (isapstr (ps));
  566. }
  567.  
  568. /*
  569.  * Copy a Pascal string into a buffer, converting it into a C string.
  570.  */
  571. p2c_string(pSource, cDestination)
  572. char *cDestination;
  573. char *pSource;
  574. {
  575.        int i;
  576.  
  577.        for (i = 0; i < pSource[0]; ++i)
  578.            cDestination[i] = pSource[i + 1];
  579.        cDestination[i] = '\0';
  580. }
  581.  
  582. /*
  583.  * Copy a OsType string into a buffer, converting it into a C string.
  584.  */
  585. Os2c_string(OsSource, cDestination)
  586. char *cDestination;
  587. OsType OsSource;
  588. {
  589.        int i;
  590.  
  591.        for (i = 0; i < 4; ++i)
  592.            cDestination[i] = OsSource.s[i];
  593.        cDestination[4] = '\0';
  594. }
  595.